<?php


namespace app\api\services;

use app\admin\model\HtnsOrderRefund;

/**
 * Class RefundOrderService
 * @package app\api\services
 * @desc 售后订单服务类
 */
class RefundOrderService
{
    use ApiTrait;

    /**
     * @desc 售后订单日志整合
     * @param int $id 售后订单id
     * @param int $oid 订单表id
     */
    public function refundLog($id , $oid = 0)
    {
        // 申请退款时间，退款类型。退款状态（同意，拒绝）时间，退款完成时间，拒绝退款时间
        $ret = [];
        $htnsOrderRefund = HtnsOrderRefund::find($id);
        if($oid){
            $where['store_order_id'] = $oid;
            $htnsOrderRefund = HtnsOrderRefund::where($where)->order('id desc')->find();
        }
        if($htnsOrderRefund['refunded_time']){
            if($htnsOrderRefund['refund_type'] == 6){
                $msg = '退款成功';
            }elseif ($htnsOrderRefund['refund_type'] == 3){
                $msg = '拒绝退款';
            }
            $ret[] = [
                'oid' => $htnsOrderRefund['store_order_id'],
                'change_type' => 'refund_price',
                'change_time' => date('Y-m-d H:i:s',$htnsOrderRefund['refunded_time']),
                'change_message' => $msg,
            ];
        }
        if($htnsOrderRefund){
            $ret[] = [
                'oid' => $htnsOrderRefund['store_order_id'],
                'change_type' => 'apply_refund',
                'change_time' => date('Y-m-d H:i:s',$htnsOrderRefund['add_time']),
                'change_message' => '申请退款-'.$htnsOrderRefund['refund_reason'],
            ];
        }
        return $ret;
    }
}